<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
class Msg extends Model{
	protected $auto = ['msg_content'];
	protected $insert = ['msg_addtime','msg_status'=>0];
	protected $update = [''];	
    public function getStatusAttr($value,$data){
        $status = [0=>'未查看',1=>'已查看',-1=>'未审核'];
        return $status[$data['msg_status']];
    }
	protected function setMsgAddtimeAttr($value,$data){
		return time();
	}	
    protected function setMsgContentAttr($value){
		$userconfig=F('_user/config');
		if($userconfig['user']['user_replace']){
		    $array = explode('|',$userconfig['user']['user_replace']);
		    $content= trim(str_replace($array,'*',strip_tags($value)));
		}else{
		    $content= strip_tags($value);
		}
		return $content;
	}
	public function user(){
		return $this->hasOne('user','user_id','msg_userid');
    }	
    //数据查询	
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='msg_'.$param['order'].' desc';
		}		
		if(empty($tag['order'])){
			$tag['order']='msg_addtime desc';
		}	
		$where=[];	

	    if(!empty($tag['uid'])) {
		    $uids = explode(',',trim($tag['uid']));
		    if(count($uids) > 1) {
			    $where['msg_uid'] = array('in',$tag['uid']);
		    }else{
			    $where['msg_uid'] = array('eq',$uids[0]);
		    }
	    }
	    if(!empty($tag['userid'])) {
		    $userid = explode(',',trim($tag['userid']));
		    if(count($uids) > 1) {
			    $where['msg_userid'] = array('in',$tag['userid']);
		    }else{
			    $where['msg_userid'] = array('eq',$userid[0]);
		    }
	    }			
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['msg_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['msg_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }	
        if(!empty($tag['page'])){
			$route=$GLOBALS['route'];
            $url=urldecode(url($route,config('params')));
		    $data = db('msg')->join('user u','u.user_id=msg_userid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = db('msg')->join('user u','u.user_id=msg_userid','LEFT')->where($where)->order($tag['order'])->limit($limit_str)->select();
		}	
		return $data;
    }		

}